package cn.tedu;

import java.sql.*;
import java.util.Scanner;

public class Demo08 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入用户名");
        String userName = in.nextLine();
        System.out.println("请输入密码");
        String password = in.nextLine();
        try ( Connection conn = DBUtils.getConn();) {
//            Statement s = conn.createStatement();
//            String sql = "select count(*) from user where username='"+userName+"' and password='"+password+"'";
//            ResultSet rs = s.executeQuery(sql);
            String sql = "select count(*) from user where username=? and password=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,userName);
            ps.setString(2,password);
            System.out.println(ps.toString());

            ResultSet rs = ps.executeQuery();
            rs.next();
            if(rs.getInt(1)>0){
                    System.out.println("登录成功!");
            }else {
                System.out.println("用户名或密码错误!");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
